package com.whfc.iot.fse.crane.processor;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.util.JSONUtil;
import com.whfc.fse.dto.FseCraneDataDTO;
import com.whfc.iot.fse.crane.entity.TwMsg;
import com.whfc.iot.fse.crane.entity.TwMsgRealData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 塔吊消息处理-实时数据
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/17 9:17
 */
@Component
public class TwUpMsgProcessorRealData implements TwUpMsgProcessor<TwMsgRealData> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void processMsg(TwMsgRealData twMsgRealData) {
        String textMsg = JSONUtil.toString(twMsgRealData);
        logger.info("处理塔吊实时数据命令:{}", textMsg);
        try {
            if (twMsgRealData == null) {
                return;
            }
            FseCraneDataDTO fseCraneDeviceData = new FseCraneDataDTO();
            fseCraneDeviceData.setPlatform(DevicePlatform.whfciot.name());
            fseCraneDeviceData.setSn(twMsgRealData.getDeviceSn()+"");
            fseCraneDeviceData.setDipAngle(twMsgRealData.getDip());
            fseCraneDeviceData.setTurnAngle(twMsgRealData.getRotation());
            fseCraneDeviceData.setHeight(twMsgRealData.getHeight());
            fseCraneDeviceData.setWeight(twMsgRealData.getWeight());
            fseCraneDeviceData.setRange(twMsgRealData.getMargin());
            fseCraneDeviceData.setTime(twMsgRealData.getDeviceTime());
            fseCraneDeviceData.setWindSpeed(twMsgRealData.getWindSpeed());
            fseCraneDeviceData.setMomentRatio(twMsgRealData.getMomentPercent() / 100);
            fseCraneDeviceData.setCraneStatus(twMsgRealData.getAlarmState());
            amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_REAL_DATA, JSONUtil.toString(fseCraneDeviceData));
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }
}
